#Region " Imports "

Imports System.Data

#End Region

Partial Class Usercontrols_Admin_Roles
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private DataPageLocal As New PagedDataSource

    Public Property sortCriteria() As String
        Get
            Return CStr(ViewState("sortCriteria"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortCriteria") = Value
        End Set
    End Property

    Public Property sortDir() As String
        Get
            Return CStr(ViewState("sortDir"))
        End Get

        Set(ByVal Value As String)
            ViewState("sortDir") = Value
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Role Management")

        If IsUserAuthenticated = True Then
            If Me.Page.IsPostBack = False Then
                LoadPage()
                LoadRoleList()
            End If

            ConfigBaseNavCtrls()

            If Me.Page.IsPostBack = False Then

                'Set Default Sort Order and Direction
                Me.sortCriteria = "username"
                Me.sortDir = "asc"

                BindDataItems()
            End If
        End If

        Me.uxAdminPanel.Visible = ShowControls(SecurityMask.Admin, True)

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveRolesForUser()
        Dim iI As Integer
        Dim iJ As Integer

        While iJ < Me.uxResultsGridView.Rows.Count
            Dim Row As GridViewRow = Me.uxResultsGridView.Rows(iJ)
            Dim UesrName As String = CType(Row.FindControl("uxUserNameHyperLink"), HyperLink).Text.ToString.Trim
            Dim Checked As Boolean = CType(Row.FindControl("uxSelectCheckBox"), CheckBox).Checked

            If Checked Then

                For iI = 0 To Me.uxRolesCheckBoxList.Items.Count - 1
                    If Me.uxRolesCheckBoxList.Items(iI).Selected = True Then
                        If Roles.IsUserInRole(UesrName, Me.uxRolesCheckBoxList.Items(iI).Value.ToString) = False Then
                            Roles.AddUserToRole(UesrName, Me.uxRolesCheckBoxList.Items(iI).Value.ToString)
                        End If
                    End If
                Next

            End If

            System.Math.Min(System.Threading.Interlocked.Increment(iJ), iJ - 1)

        End While

    End Sub

    Private Sub RemoveRolesForUser()
        Dim iI As Integer
        Dim iJ As Integer

        While iJ < Me.uxResultsGridView.Rows.Count
            Dim Row As GridViewRow = Me.uxResultsGridView.Rows(iJ)
            Dim UesrName As String = CType(Row.FindControl("uxUserNameHyperLink"), HyperLink).Text.ToString.Trim
            Dim Checked As Boolean = CType(Row.FindControl("uxSelectCheckBox"), CheckBox).Checked

            If Checked Then

                For iI = 0 To Me.uxRolesCheckBoxList.Items.Count - 1
                    If Me.uxRolesCheckBoxList.Items(iI).Selected = True Then
                        If Roles.IsUserInRole(UesrName, Me.uxRolesCheckBoxList.Items(iI).Value.ToString) = True Then
                            Roles.RemoveUserFromRole(UesrName, Me.uxRolesCheckBoxList.Items(iI).Value.ToString)
                        End If
                    End If
                Next

            End If

            System.Math.Min(System.Threading.Interlocked.Increment(iJ), iJ - 1)

        End While

    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadPageSize(Me.uxPageSizeDropDownList)
    End Sub

    Private Sub LoadRoleList()
        With Me.uxRolesCheckBoxList
            .DataSource = Roles.GetAllRoles
            .DataBind()
        End With
    End Sub

    Public Overloads Overrides Sub BindDataItems()

        Dim Users As New MembershipUtility
        Dim DS As New DataSet
        Dim DT As New DataTable
        Dim DV As New DataView

        DS = Users.GetAllUsers

        Me.RecordCount = DS.Tables(0).Rows.Count

        DT = DS.Tables(0)

        DV.Table = DT

        DV.Sort = Me.sortCriteria & " " & Me.sortDir

        Me.DataPage.DataSource = DV
        Me.DataPage.AllowPaging = True
        Me.DataPage.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.DataPage.CurrentPageIndex = Me.PageNumber

        CreatePagingLinks()

        With Me.uxResultsGridView
            .DataSource = Me.DataPage
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxCreateRoleButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxCreateRoleButton.Click
        Roles.Provider.CreateRole(Me.uxRoleNameTextBox.Text.ToString.Trim)

        LoadRoleList()
    End Sub

    Protected Sub uxRemoveRolesButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxRemoveRolesButton.Click

        If (LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "admin" Or _
        LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "editor" Or _
        LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "reviewer" Or _
        LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "template" Or _
        LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "moderator" Or _
        LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "blogmoderator" Or _
        LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "pollmoderator" Or _
        LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "eventmoderator" Or _
        LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "gallerymoderator" Or _
        LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "member") Or _
        LCase(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim) = "guest" Then

            ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "alert", "window.alert('System roles are not allowed to be deleted!');", True)

        Else

            If Roles.GetUsersInRole(Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim).Length > 0 Then
                Roles.RemoveUsersFromRole(Roles.GetUsersInRole(Me.uxRolesCheckBoxList.SelectedItem.Text.ToString.Trim), Me.uxRolesCheckBoxList.SelectedItem.ToString.Trim)
            End If

            Roles.DeleteRole(Me.uxRolesCheckBoxList.SelectedItem.Text.ToString.Trim, False)

            LoadRoleList()

        End If
    End Sub

    Protected Sub uxAddRolesButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddRolesButton.Click
        SaveRolesForUser()
        BindDataItems()
        LoadRoleList()
    End Sub

    Protected Sub uxRemoveUserRolesButtonButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxRemoveUserRolesButton.Click
        RemoveRolesForUser()
        BindDataItems()
        LoadRoleList()
    End Sub

    Protected Sub uxResultsGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles uxResultsGridView.Sorting
        'Get the Sort by Value from the Sortexpression field on the column
        If Me.sortCriteria = e.SortExpression Then
            If Me.sortDir = "desc" Then
                Me.sortDir = "asc"
            Else
                Me.sortDir = "desc"
            End If
        End If

        ' Assign the column clicked to the sortCriteria property
        Me.sortCriteria = e.SortExpression

        'Calls the function to rebuild the datagrid view, with the new sort order
        BindDataItems()
    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub ConfigBaseNavCtrls()

        Me.PagingTopPanel = Me.uxPagingTopPanel
        Me.PagingBottomPanel = Me.uxPagingBottomPanel

        Me.RepeaterTop = Me.uxPagesTopRepeater
        Me.RepeaterBottom = Me.uxPagesBottomRepeater

        Me.NextTopLinkButton = Me.uxTopNextLinkButton
        Me.PreviousTopLinkButton = Me.uxTopPrevLinkButton
        Me.NextBottomLinkButton = Me.uxBottomNextLinkButton
        Me.PreviousBottomLinkButton = Me.uxBottomPrevLinkButton

        Me.DataPage = DataPageLocal
        Me.PageSize = CType(Me.uxPageSizeDropDownList.SelectedValue.ToString, Integer)
        Me.PagesToDisplay = 10

        Me.ItemsPerPageDropDown = Me.uxPageSizeDropDownList
        Me.ItemsPerPageLabel = Me.uxItemsPerPageLabel
        Me.PagerInfoLabel = Me.uxPagerInfoLabel

        AddHandler Me.RepeaterTop.ItemCommand, AddressOf PagingRepeater_ItemCommand
        AddHandler Me.RepeaterBottom.ItemCommand, AddressOf PagingRepeater_ItemCommand

    End Sub

    Protected Function GetRolesForUser(ByVal UserID As String) As String
        Dim RoleList As New StringBuilder
        Dim RoleItem As Object

        For Each RoleItem In Roles.GetRolesForUser(UserID)
            RoleList.Append(RoleItem.ToString & ", ")
        Next

        If RoleList.Length > 0 Then
            If Right(RoleList.ToString, 2) = ", " Then
                RoleList.Length = RoleList.Length - 2
            End If
        End If

        Return RoleList.ToString

    End Function

#End Region

End Class

